<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove"
	xmlns:uc1="remove">
	<xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"></xsl:output>
	<xsl:template match="/">
		<xsl:text><![CDATA[<%@ Register TagPrefix="uc1" TagName="MultipleSelection" Src="~/FormsManagement/Controls/MultipleSelection.ascx" %>]]></xsl:text>
		<xsl:text><![CDATA[<%@ Register TagPrefix="uc1" TagName="DropDownMultipleSelection" Src="~/FormsManagement/Controls/DropDownMultipleSelection.ascx" %>]]></xsl:text>
		<xsl:text><![CDATA[<%@ Register TagPrefix="uc1" TagName="CustomValueSelector" Src="~/FormsManagement/Controls/CustomValueSelector.ascx" %>]]></xsl:text>
		<xsl:text><![CDATA[<LINK href="../../Insight21.css" type="text/css" rel="stylesheet">]]></xsl:text>
		<table cellpadding="0" cellspacing="0" id="menu" width="100%">
			<xsl:for-each select="Form/FormItems/FormItem">
				<xsl:call-template name="ItemTemplate">
				</xsl:call-template>
			</xsl:for-each>
		</table>
	</xsl:template>
	
	<xsl:template name="ItemTemplate">
		<xsl:if test="HasSeparator='true'">		<!-- separator row -->
			<tr>
				<td colspan="2"> <!-- colspan of 2 is becoz of if the question is to be indented, then a column with a clear.gif image is added to the row in which the question is dispalyed -->
					<hr></hr>
				</td>
			</tr>
		</xsl:if>
		<tr>
			
			<xsl:if test="Indent='true'">	<!-- if question should be indented -->
				<td>
					<xsl:element name="img">
						<xsl:attribute name="src">../Images/clear.gif</xsl:attribute>
						<xsl:attribute name="width"><xsl:value-of select="20" /></xsl:attribute>
					</xsl:element>
				</td>
			</xsl:if>
			<xsl:element name="td">		<!-- display question and recursively call the template for its children, if any -->
				<xsl:attribute name="width">100%</xsl:attribute>
				<xsl:if test="Indent='false'"><xsl:attribute name="colspan">2</xsl:attribute></xsl:if>	<!-- set colspan=2 for questions which are not indented -->
				<table width="100%">
					<tr>
						<td width="100%">
							<xsl:element name="a">
								<xsl:attribute name="name">
									<xsl:value-of select="Name"/>
								</xsl:attribute>
							</xsl:element>
							<xsl:if test="Highlight='true'">
								<font style="FONT-SIZE: 14pt; COLOR: red; FONT-FAMILY: 'Wingdings'">F</font>
								<font style="COLOR: red; FONT-WEIGHT: bold">
									<xsl:value-of select="ValidationMessage" />
								</font>
								<br />
							</xsl:if>
							<xsl:value-of select="Text" />
						</td>
					</tr>
					<xsl:choose>
						<xsl:when test="Type = 'RadioButtonList' or Type='DropDownList'">
							<tr>
								<td>
									<xsl:element name="INPUT">
										<xsl:attribute name="id">
											<xsl:value-of select="concat(Name, 'Tracker')" />
										</xsl:attribute>
										<xsl:attribute name="type">hidden</xsl:attribute>
										<xsl:if test="Answers/Answer">
											<xsl:attribute name="value"><xsl:value-of select="concat(Name, Answers/Answer[Selected = 'true']/Answer)" /></xsl:attribute>
										</xsl:if>
									</xsl:element>
								</td>
							</tr>
							<tr>
								<td height="20" nowrap="">
									<xsl:element name="asp:{Type}">
										<xsl:attribute name="id">
											<xsl:value-of select="Name" />
										</xsl:attribute>
										<xsl:attribute name="runat">server</xsl:attribute>
										<xsl:if test="Type='DropDownList'"><xsl:attribute name="CssClass">FlatDropDown</xsl:attribute></xsl:if>
										<xsl:for-each select="./ItemProperties/ItemProperty">
											<xsl:attribute name="{AttributeName}">
												<xsl:value-of select="AttributeValue"></xsl:value-of>
											</xsl:attribute>
										</xsl:for-each>
										<xsl:if test="Type='DropDownList'">
											<asp:ListItem value="Select">-- Select --</asp:ListItem>
										</xsl:if>
										<xsl:for-each select="./ItemOptions/ItemOption">
											<xsl:choose>
												<xsl:when test="../../Answers/Answer[Selected = 'true']/Answer = ./OptionValue">
													<asp:ListItem value="{OptionValue}" selected="">
														<xsl:value-of select="OptionText" />
													</asp:ListItem>
												</xsl:when>
												<xsl:otherwise>
													<asp:ListItem value="{OptionValue}">
														<xsl:value-of select="OptionText" />
													</asp:ListItem>
												</xsl:otherwise>
											</xsl:choose>
										</xsl:for-each>
									</xsl:element>
								</td>
							</tr>
						</xsl:when>
						<xsl:when test="Type='CheckBoxList'">
							<tr>
								<td>
									<xsl:element name="INPUT">
										<xsl:attribute name="id">
											<xsl:value-of select="concat(Name, 'Tracker')" />
										</xsl:attribute>
										<xsl:attribute name="type">hidden</xsl:attribute>
									</xsl:element>
								</td>
							</tr>
							<xsl:for-each select="./ItemOptions/ItemOption">
								<tr>
									<td height="20">
										<xsl:element name="asp:CheckBox">
											<xsl:attribute name="id">
												<xsl:value-of select="concat(../../Name, OptionValue)" />
											</xsl:attribute>
											<xsl:attribute name="runat">server</xsl:attribute>
											<xsl:attribute name="Text">
												<xsl:value-of select="OptionText" />
											</xsl:attribute>
											<xsl:variable name="optionValueVariable">
												<xsl:value-of select="OptionValue" />
											</xsl:variable>
											<xsl:if test="../../Answers/Answer[Answer = $optionValueVariable]/Selected = 'true'">
												<xsl:attribute name="checked">true</xsl:attribute>
											</xsl:if>
										</xsl:element>
									</td>
								</tr>
							</xsl:for-each>
						</xsl:when>
						<xsl:when test="Type='TextBox'">
							<tr>
								<td height="20" nowrap="">
									<xsl:element name="asp:{Type}">
										<xsl:attribute name="id">
											<xsl:value-of select="Name" />
										</xsl:attribute>
										<xsl:attribute name="runat">server</xsl:attribute>
										<xsl:attribute name="CssClass">FlatTextBox</xsl:attribute>
										<xsl:for-each select="./ItemProperties/ItemProperty">
											<xsl:attribute name="{AttributeName}">
												<xsl:value-of select="AttributeValue"></xsl:value-of>
											</xsl:attribute>
										</xsl:for-each>
										<xsl:if test = "not(./ItemProperties/ItemProperty[AttributeName = 'MaxLength'])">
											<xsl:attribute name="MaxLength">4000</xsl:attribute>
										</xsl:if>
										<xsl:value-of select="Answers/Answer[Deleted='false']/Answer" />
									</xsl:element>
								</td>
							</tr>
						</xsl:when>
						<xsl:when test="Type = 'MultiInput'">
							<tr>
								<td height="20" nowrap="">
									<uc1:MultipleSelection runat="server">
										<xsl:attribute name="id">
											<xsl:value-of select="Name" />
										</xsl:attribute>
										<xsl:for-each select="./ItemProperties/ItemProperty">
											<xsl:attribute name="{AttributeName}">
												<xsl:value-of select="AttributeValue"></xsl:value-of>
											</xsl:attribute>
										</xsl:for-each>
										<xsl:attribute name="ParentName">
											<xsl:value-of select="ParentName" />
										</xsl:attribute>
										<xsl:attribute name="ParentValue">
											<xsl:value-of select="ParentValue" />
										</xsl:attribute>
										<xsl:variable name="answersVariable">
											<xsl:for-each select="Answers/Answer[Deleted='false']"><xsl:value-of select="Answer" />^</xsl:for-each>
										</xsl:variable>
										<xsl:attribute name="Items">
											<xsl:value-of select="substring($answersVariable, 1, string-length($answersVariable)-1)" />
										</xsl:attribute>
										<xsl:attribute name="SelectedListItemText">
											<xsl:value-of select="Answers/Answer[Selected = 'true']/Answer" />
										</xsl:attribute>
									</uc1:MultipleSelection>
								</td>
							</tr>
						</xsl:when>
						<xsl:when test="Type = 'DropDownMultiSelect'">
							<tr>
								<td height="20" nowrap="">
									<uc1:DropDownMultipleSelection runat="server">
										<xsl:attribute name="id">
											<xsl:value-of select="Name" />
										</xsl:attribute>
										<xsl:attribute name="ParentName">
											<xsl:value-of select="ParentName" />
										</xsl:attribute>
										<xsl:attribute name="ParentValue">
											<xsl:value-of select="ParentValue" />
										</xsl:attribute>
										<xsl:variable name="answersVariable">
											<xsl:for-each select="Answers/Answer[Deleted='false']"><xsl:value-of select="Answer" />^</xsl:for-each>
										</xsl:variable>
										<xsl:attribute name="Items">
											<xsl:value-of select="substring($answersVariable, 1, string-length($answersVariable)-1)" />
										</xsl:attribute>
										<xsl:variable name="textStringsVariable">
											<xsl:for-each select="ItemOptions/ItemOption"><xsl:value-of select="OptionText" />^</xsl:for-each>
										</xsl:variable>
										<xsl:attribute name="DropDownListItemTextStrings">
											<xsl:value-of select="substring($textStringsVariable, 1, string-length($textStringsVariable)-1)" />
										</xsl:attribute>
										<xsl:variable name="valuesVariable">
											<xsl:for-each select="ItemOptions/ItemOption"><xsl:value-of select="OptionValue" />^</xsl:for-each>
										</xsl:variable>
										<xsl:attribute name="DropDownListItemValues">
											<xsl:value-of select="substring($valuesVariable, 1, string-length($valuesVariable)-1)" />
										</xsl:attribute>
										<xsl:attribute name="SelectedListItemText">
											<xsl:value-of select="Answers/Answer[Selected = 'true']/Answer" />
										</xsl:attribute>
									</uc1:DropDownMultipleSelection>
								</td>
							</tr>
						</xsl:when>
						<xsl:when test="Type = 'CustomValueSelector'">
							<tr>
								<td height="20" nowrap="">
									<uc1:CustomValueSelector runat="server">
										<xsl:attribute name="id">
											<xsl:value-of select="Name" />
										</xsl:attribute>
										<xsl:for-each select="./ItemProperties/ItemProperty">
											<xsl:attribute name="{AttributeName}">
												<xsl:value-of select="AttributeValue"></xsl:value-of>
											</xsl:attribute>
										</xsl:for-each>
										<xsl:attribute name="ParentName">
											<xsl:value-of select="ParentName" />
										</xsl:attribute>
										<xsl:attribute name="ParentValue">
											<xsl:value-of select="ParentValue" />
										</xsl:attribute>
										<xsl:variable name="answersVariable">
											<xsl:for-each select="Answers/Answer[Deleted='false']"><xsl:value-of select="Answer" />^</xsl:for-each>
										</xsl:variable>
										<xsl:attribute name="ItemValues">
											<xsl:value-of select="substring($answersVariable, 1, string-length($answersVariable)-1)" />
										</xsl:attribute>
										<xsl:attribute name="SelectedListItemValue">
											<xsl:value-of select="Answers/Answer[Selected = 'true']/Answer" />
										</xsl:attribute>
									</uc1:CustomValueSelector>
								</td>
							</tr>
						</xsl:when>
					</xsl:choose>
				</table>
			</xsl:element>
		</tr>
		
		<!-- include child questions -->
		<xsl:if test="Type = 'RadioButtonList' or Type='DropDownList' or Type='CheckBoxList'">
			<xsl:if test="FormItems/FormItem">
				<xsl:for-each select="ItemOptions/ItemOption">
					<xsl:variable name="optionValueVariable">
						<xsl:value-of select="OptionValue" />
					</xsl:variable>
					<xsl:variable name="optionTextVariable">
						<xsl:value-of select="OptionText" />
					</xsl:variable>
					<xsl:if test="../../FormItems/FormItem[ParentValue = $optionValueVariable]">
						&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;
						&lt;table id=&quot;<xsl:value-of select="concat(../../Name, OptionValue)" />&quot; width=&quot;100&#37;&quot;
						<xsl:if test="not(../../Answers/Answer) or  not(../../Answers/Answer[Selected ='true' and Answer = $optionValueVariable])">
							style=&quot;display:none;&quot;
						</xsl:if>
						&gt;
						<xsl:for-each select="../../FormItems/FormItem[ParentValue = $optionValueVariable]">
							<xsl:call-template name="ItemTemplate">
							</xsl:call-template>
						</xsl:for-each>
						&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;
					</xsl:if>
				</xsl:for-each>
			</xsl:if>
		</xsl:if>
		
		<!-- for multiselect and dropdownmultiselect, include only those children which correspond to the answer selected in the multiselect and dropdownmultiselect -->
		<xsl:if test="Type='MultiInput' or Type='DropDownMultiSelect'">
			<xsl:if test="FormItems/FormItem">
				<xsl:for-each select="./Answers/Answer[Selected ='true']">
					<xsl:variable name="answerVariable">
						<xsl:value-of select="Answer" />
					</xsl:variable>
					<xsl:if test="../../FormItems/FormItem[ParentValue = $answerVariable]">
						&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;
						&lt;table id=&quot;<xsl:value-of select="concat(../../Name, Answer)" />&quot; width=&quot;100&#37;&quot;
						&gt;
						<xsl:for-each select="../../FormItems/FormItem[ParentValue = $answerVariable]">
							<xsl:call-template name="ItemTemplate">
							</xsl:call-template>
						</xsl:for-each>
						&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;
					</xsl:if>
				</xsl:for-each>
			</xsl:if>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>
